<?php
/**
 * Created by PhpStorm.
 * User: lwh
 * Date: 2017/7/4
 * Time: 18:48
 */
    include_once "connect.php";
    include_once "weixinUtil.php";

    $is_ajax = $_REQUEST['is_ajax'];
    if(isset($is_ajax) && $is_ajax) {
        $username = $_REQUEST['username'];
        $password = $_REQUEST['password'];
        $uesrId = $_REQUEST['userId'];

        //mysql查询
        $sql = "select * from user where username = '{$username}' and password = '{$password}'";
        $conn = new db();
        $db = $conn->getDBConn();
        $memcache = $conn->getMemcached();
        $row = $db->query($sql)->fetch(PDO::FETCH_ASSOC);//调用查询函数query(),并以关联数组的形式储存

        //用户名密码存在，查询获得用户信息，将微信的openId存储到数据库
        if($row){
            $tool = new weixinTool("HPQwjnca_An3LjptuP8zdQWvxkOANDn3FhLygAHLwe0");

            if($memcache->get('access_token')){
                $user = $tool->get_user_info($uesrId);
                $sql = "update user set weixinId = '{$uesrId}' where userId = '{$row['userId']}'";
                $rows = $db->exec($sql);
                $data = array();
                $data["content"] = "恭喜您成功绑定CrowdFrame的用户信息。后续有新任务发布或者您发布的任务有进度更新，都会通过微信端推送消息，方便您的使用。";
                $tool->send_message('text',1,$uesrId,$data);
                echo "success";
            }
            else{
                echo "get access token error";
            }
        }
        else
            echo "fail";
    }
?>
